package com.xx.happychatboot.serve.user.service.impl;

import com.xx.happychatboot.base.vo.response.ResponseDate;
import com.xx.happychatboot.serve.user.domain.vo.response.AddUserResponse;
import com.xx.happychatboot.serve.user.mapper.SysUserInfoMapper;
import com.xx.happychatboot.serve.user.service.ISysUserInfoService;
import com.xx.happychatboot.serve.user.domain.entity.SysUserInfo;
import io.mybatis.service.AbstractService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class SysUserInfoServiceImpl extends AbstractService<SysUserInfo,Long, SysUserInfoMapper>
        implements ISysUserInfoService {
    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Override
    public ResponseDate getUserInfo(Long userId) {
        if (Objects.isNull(userId)) {
            return ResponseDate.fail("获取用户信息失败");
        }
        SysUserInfo sysUserInfo = new SysUserInfo();
        sysUserInfo.setUserId(userId);
        Optional<SysUserInfo> userInfo = sysUserInfoMapper.selectOne(sysUserInfo);

        return userInfo.map(info -> ResponseDate.success("获取用户信息成功", info)).orElseGet(() -> ResponseDate.fail("获取用户信息失败"));

    }

    /**
     * 更新用户信息
     * @param userInfo
     * @return
     */
    @Override
    public ResponseDate updateUserInfo(SysUserInfo userInfo) {

        if (Objects.isNull(userInfo)
                || Objects.isNull(userInfo.getUserId())
                || Objects.isNull(userInfo.getUserInfoId())) {
            return ResponseDate.fail("保存失败，请联系管理员");
        }

        sysUserInfoMapper.updateByPrimaryKeySelective(userInfo);

        return ResponseDate.success("保存成功");
    }

    /**
     * 通过用户账号查询用户信息
     * @param userName
     * @return
     */
    @Override
    public ResponseDate searchUserByUserName(String userName) {
        if (StringUtils.isBlank(userName)) {
            return ResponseDate.fail("搜索框信息不可为空");
        }

        List<AddUserResponse> addUserResponse = sysUserInfoMapper.searchUserByUserName(userName);

        return ResponseDate.success(addUserResponse);
    }
}
